<?php
require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_good_upload_action_service extends background_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        //var_dump(__METHOD__); exit();
                
        $result = array('status' => 0, 'message' => '', 'data' => array());
	     
	    $id        = init_user_data_key($_POST, 'id', 'int');
	    $timestamp = init_user_data_key($_POST, 'timestamp', 'str');
	    $token     = init_user_data_key($_POST, 'token', 'str');
	    
	    if($id === null || $id < 0){
	        $id = 0;
	    }
	    	    
	    $uploadify_key = wwy_resource_container::get_config_item('uploadify_key');
	    $verify_token = md5($timestamp . $uploadify_key);
	    if ((empty($_FILES['Filedata']['tmp_name'])) || ($token !== $verify_token)) {    	        
	        $result['status']  = -400;
	        $result['message'] = '上传失败';
	    }else{	  	    
    	    $tmp_name = $_FILES['Filedata']['tmp_name'];
    	    $image_info = @getimagesize($tmp_name);
    	    if($image_info == false){
    	        $result['status']  = -400;
    	        $result['message'] = '上传失败，请上传有效图片文件';    	        
    	    }else{
    	        $image_type_ext_lookup = array(
    	            IMAGETYPE_JPEG => 'jpg',
    	            IMAGETYPE_PNG  => 'png',
    	            IMAGETYPE_GIF  => 'gif',    	            
    	        );
    	        $image_type = isset($image_info[2]) ? $image_info[2] : null;
    	        if(!isset($image_type_ext_lookup[$image_type])){
    	            $result['status']  = -400;
    	            $result['message'] = '图片格式只能为jpg, png, gif';
    	        }else{
    	            $ext = $image_type_ext_lookup[$image_type];
    	            
    	            $file_handler  = wwy_resource_container::get_file_handler();
    	            $image_handler = wwy_resource_container::get_image_handler();
    	             
    	            $vary_dir = date('Ym');
    	            $good_dir = PROJECT_ROOT_PATH . '/public/module/common/upload/images/good/';
    	            $image    = $vary_dir. '/origin/' . uniqid('cdnhost' . '_' . $id . '_', true) . '.' . $ext;
    	            $target   = $good_dir . $image;
    	             
    	            $upload_result = $file_handler->upload($tmp_name, $target);
    	            if($upload_result['status'] < 0){
    	                //return $upload_result;
    	                $result['status']  = $upload_result['status'];
    	                $result['message'] = $upload_result['message'];
    	            }else{
    	                 
    	                //创建标准主图
    	                $norm_target = str_replace('/origin/', '/norm/', $target);
    	                $file_handler->mk_dir(dirname($norm_target));
    	                $image_handler->mk_thumb($target, 400, 400, $norm_target, $ext);
    	        
    	                //创建列表缩略图
    	                $thumb_target = str_replace('/origin/', '/thumb/', $target);
    	                $file_handler->mk_dir(dirname($thumb_target));
    	                $image_handler->mk_thumb($target, 180, 180, $thumb_target, $ext);
    	        
    	                $image_origin = '/module/common/upload/images/good/' . $image;
    	                $image_thumb  = '/module/common/upload/images/good/' . str_replace('/origin/', '/thumb/', $image);
    	                $image_norm   = '/module/common/upload/images/good/' . str_replace('/origin/', '/norm/', $image);
    	                 
    	                $result['status']  = 1;
    	                $result['message'] = '上传成功';
    	                self::add_view_data('image', $image);
    	                self::add_view_data('image_origin', $image_origin);
    	                self::add_view_data('image_thumb', $image_thumb);
    	                self::add_view_data('image_norm', $image_norm);
    	            }
    	        }   	        
    	    }
	    }
		
		self::set_view_status($result['status']);
		self::set_view_message($result['message']);
    }
}